using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Thunder.IrcClient
{
    /// <summary>
    /// Helper class for RichTextbox vertical scroll
    /// </summary>
    internal static class VerticalScrollHelper
    {
        /// <summary>
        /// Resets the vertical scroll to fit the text if the text was longer than any previous text
        /// </summary>
        /// <param name="richTextBox">The <see cref="RichTextBox"/> to reset</param>
        /// <param name="message">The message to measure</param>
        internal static void ResetVerticalScroll(RichTextBox richTextBox, string message)
        {
            FormattedText formattedText = new FormattedText(message, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(richTextBox.Document.FontFamily.Source), richTextBox.Document.FontSize + 4, Brushes.Black);
            double width = formattedText.Width;
            if (double.IsNaN(richTextBox.Document.PageWidth) == true || richTextBox.Document.PageWidth < width)
            {
                richTextBox.Document.PageWidth = width;
            }
        }
    }
}